package com.yanqu.road.dao.impl.player;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.qixi.UserQixiRelationData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class UserQixiRelationDaoImpl extends TempDataObjectDao<UserQixiRelationData> {
    public static String getUserQixiRelationDataSql = "select * from `t_u_qixi_relation` where `activityId`=? and `userId`=?";

    public Map<Long, UserQixiRelationData> getUserQixiRelationDataMap(int activityId, long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Long, UserQixiRelationData> dataMap = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getUserQixiRelationDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setLong(i++, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                UserQixiRelationData data = parse(rs);
                dataMap.put(data.getTargetUserId(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }

    public static String updateUserQixiRelationDataSql = "update `t_u_qixi_relation` set getFlowerCount=?,sendFlowerCount=?,getTime=?,sendTime=? where activityId=? and userId=? and targetUserId=?";

    @Override
    protected boolean updateData(UserQixiRelationData userQixiRelationData) {
        userQixiRelationData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, userQixiRelationData.getGetFlowerCount()));
        para.put(i++, new DbParameter(Types.INTEGER, userQixiRelationData.getSendFlowerCount()));
        para.put(i++, new DbParameter(Types.BIGINT, userQixiRelationData.getGetTime()));
        para.put(i++, new DbParameter(Types.BIGINT, userQixiRelationData.getSendTime()));
        para.put(i++, new DbParameter(Types.INTEGER, userQixiRelationData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, userQixiRelationData.getUserId()));
        para.put(i++, new DbParameter(Types.BIGINT, userQixiRelationData.getTargetUserId()));
        if (getDb().executeNoneQuery(updateUserQixiRelationDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertUserQixiRelationDataSql = "insert into `t_u_qixi_relation`(activityId,userId,targetUserId,getFlowerCount,sendFlowerCount,getTime,sendTime) values (?,?,?,?,?,?,?)";

    @Override
    protected boolean addData(UserQixiRelationData userQixiRelationData) {
        userQixiRelationData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, userQixiRelationData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, userQixiRelationData.getUserId()));
        para.put(i++, new DbParameter(Types.BIGINT, userQixiRelationData.getTargetUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, userQixiRelationData.getGetFlowerCount()));
        para.put(i++, new DbParameter(Types.INTEGER, userQixiRelationData.getSendFlowerCount()));
        para.put(i++, new DbParameter(Types.BIGINT, userQixiRelationData.getGetTime()));
        para.put(i++, new DbParameter(Types.BIGINT, userQixiRelationData.getSendTime()));
        if (getDb().executeNoneQuery(insertUserQixiRelationDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected UserQixiRelationData parseData(ResultSet rs) throws SQLException {
        UserQixiRelationData userQixiRelationData = new UserQixiRelationData();
        userQixiRelationData.setActivityId(rs.getInt("activityId"));
        userQixiRelationData.setUserId(rs.getLong("userId"));
        userQixiRelationData.setTargetUserId(rs.getLong("targetUserId"));
        userQixiRelationData.setGetFlowerCount(rs.getInt("getFlowerCount"));
        userQixiRelationData.setSendFlowerCount(rs.getInt("sendFlowerCount"));
        userQixiRelationData.setGetTime(rs.getLong("getTime"));
        userQixiRelationData.setSendTime(rs.getLong("sendTime"));

        return userQixiRelationData;
    }
}
